package xyz.o0o0o.demo.AuthorizationServerDemo2.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

public class CustomClientDetails implements ClientDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private OAuthClientDetails oAuthClientDetails;

	public CustomClientDetails(OAuthClientDetails oAuthClientDetails) {
		this.oAuthClientDetails = oAuthClientDetails;
	}

	public String getClientId() {
		return oAuthClientDetails.getClient_id();
	}

	public Set<String> getResourceIds() {
		Set<String> resourceIds = new HashSet<String>();
		if (oAuthClientDetails.getResource_ids() != null) {
			String[] resourceIdsTemp = oAuthClientDetails.getResource_ids().split(",");
			for (String resourceId : resourceIdsTemp) {
				resourceIds.add(resourceId);
			}
		}
		return resourceIds;
	}

	public boolean isSecretRequired() {
		return true;
	}

	public String getClientSecret() {
		return oAuthClientDetails.getClient_secret();
	}

	public boolean isScoped() {
		return true;
	}

	public Set<String> getScope() {
		Set<String> scopes = new HashSet<String>();
		if (oAuthClientDetails.getScope() != null) {
			String[] scopesTemp = oAuthClientDetails.getScope().split(",");
			for (String scope : scopesTemp) {
				scopes.add(scope);
			}
		}
		return scopes;
	}

	public Set<String> getAuthorizedGrantTypes() {
		Set<String> types = new HashSet<String>();
		if (oAuthClientDetails.getAuthorized_grant_types() != null) {
			String[] typesTemp = oAuthClientDetails.getAuthorized_grant_types().split(",");
			for (String type : typesTemp) {
				types.add(type);
			}
		}
		return types;
	}

	public Set<String> getRegisteredRedirectUri() {
		Set<String> urls = new HashSet<String>();
		if (oAuthClientDetails.getWeb_server_redirect_uri() != null) {
			String[] urlsTemp = oAuthClientDetails.getWeb_server_redirect_uri().split(",");
			for (String url : urlsTemp) {
				urls.add(url);
			}
		}
		return urls;
	}

	public Collection<GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		if (oAuthClientDetails.getAuthorities() != null) {
			String[] authoritiesTemp = oAuthClientDetails.getAuthorities().split(",");
			for (String authority : authoritiesTemp) {
				authorities.add(new SimpleGrantedAuthority(authority));
			}
		}
		return authorities;
	}

	public Integer getAccessTokenValiditySeconds() {
		// TODO Auto-generated method stub
		return 1000;
	}

	public Integer getRefreshTokenValiditySeconds() {
		// TODO Auto-generated method stub
		return 1000;
	}

	public boolean isAutoApprove(String scope) {
		// TODO Auto-generated method stub
		return true;
	}

	public Map<String, Object> getAdditionalInformation() {
		// TODO Auto-generated method stub
		return new HashMap<String,Object>();
	}

}
